"""
基础接口封装
"""
from common.commonbase import CommonRequests
import ast
from common.commonfunc import CommonLog, get_config, get_data_by_dict


class ApiCrmebBase(CommonRequests):
    """
    基础接口
    """

    api_url = None
    api_method = None
    api_params_key = None
    api_params_data = None
    api_headers = None
    api_body_data = None

    def __init__(self, api_path, env):
        super(ApiCrmebBase, self).__init__(api_path=api_path, env=env)

    def get_base_api_config(self, api_name):
        """
            获取基础接口配置
        """
        self.api_log.log_info(f'api_name:{api_name}')

        return self.get_business_config(api_name, 'baseapi')

    def admin_login(self):
        """
                   管理员登录
        """
        base_api_content = self.get_base_api_config('admin_login')
        self.api_url = get_data_by_dict(base_api_content, 'api_path')
        self.api_log.log_info(f'api_url:{self.api_url}')
        self.api_method = get_data_by_dict(base_api_content, 'method')
        self.api_log.log_info(f'api_method:{self.api_method}')
        self.api_headers = get_data_by_dict(base_api_content, 'headers')
        self.api_log.log_info(f'api_headers:{self.api_headers}')
        self.api_body_data = get_data_by_dict(base_api_content, 'body_data')
        self.api_log.log_info(f'api_params_data:{self.api_params_data}')
        resp = self.send_request(url=self.api_url, method=self.api_method,
                                 data=self.api_body_data)
        if resp.status_code == 200:
            return resp.json()

    def admin_logout(self):
        """
                       管理员退出
            """
        base_api_content = self.get_base_api_config('admin_logout')
        self.api_url = get_data_by_dict(base_api_content, 'api_path')
        self.api_log.log_info(f'api_url:{self.api_url}')
        self.api_method = get_data_by_dict(base_api_content, 'method')
        self.api_log.log_info(f'api_method:{self.api_method}')
        self.api_headers = get_data_by_dict(base_api_content, 'headers')
        self.api_log.log_info(f'api_headers:{self.api_headers}')
        resp = self.send_request(url=self.api_url, method=self.api_method, headers=self.api_headers)
        self.api_log.log_info(f'resp:{resp}')
        if resp.status_code == 200:
            self.api_log.log_info(f'resp:{resp.json()}')
            return resp.json()

